package
{
	import flash.geom.Matrix;
	import flash.geom.Matrix3D;
	import flash.geom.Point;
	import flash.geom.Vector3D;

	public class IsometricTransformer implements ITransformer
	{
		private var m_isoMatrix:Matrix3D;
		private var m_reverseMatrix:Matrix3D;
		
		public static const PPC_X:Number = 20;
		public static const PPC_Y:Number = 20;
		public static const PPC_Z:Number = 20;
		public function IsometricTransformer()
		{
			var _cos:Number = Math.cos(45*Math.PI/180);
			var _sin:Number = Math.sin(45*Math.PI/180);
			
			m_isoMatrix = new Matrix3D();
			
			m_isoMatrix.appendScale(PPC_X,PPC_Y,PPC_Z);
			m_isoMatrix.appendRotation( 90, Vector3D.X_AXIS );			
			m_isoMatrix.appendRotation( 135, Vector3D.Y_AXIS );
			m_isoMatrix.appendRotation( -35.264, Vector3D.X_AXIS );			
			
			var test:Matrix3D = m_isoMatrix.clone();		
			test.invert();
			
			m_reverseMatrix = m_isoMatrix.clone();
			var temp:Vector.<Number> = m_reverseMatrix.rawData;
			//flattening the z values of the iso map onto the screen (for pixel to tile mapping)
			// project to z = 0 plane
			temp[2] = 0;
			temp[6] = 0;
			temp[10] = 1;
			m_reverseMatrix = new Matrix3D(temp);
			m_reverseMatrix.invert();
		}
		
		public function transform(pos:Vector3D):Vector3D{
			return m_isoMatrix.transformVector(pos);
		}
		
		public function reverseTransform(pos:Vector3D):Vector3D{
			var reverse:Vector3D = m_reverseMatrix.transformVector(pos);
			var result:Vector3D = reverse.clone();
			
			return result;
		}
	}
}